package com.itheima.reggie.exception;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author kcs
 * @date 2021/11/22
 * @TIME:11:47
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R ex1(SQLIntegrityConstraintViolationException ex){
        log.info("拦截到添加重复数据异常...");
        return R.error("该数据已存在");
    }

    @ExceptionHandler(CustomException.class)
    public R ex2(CustomException ex){
        log.info("拦截到业务异常...");
        return R.error(ex.getMessage());
    }

}
